﻿Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Text
Imports Excel = Microsoft.Office.Interop.Excel
Public Class QuanLyNguoiChoThue

    Private Sub QuanLyNguoiChoThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bang As DataTable = NhaChoThue_BUS.loadDuLieuNguoiChoThue()
        DataGridView1_QLNguoiChoThue.DataSource = bang
        DataGridView1_QLNguoiChoThue.AutoResizeColumns()
        DataGridView1_QLNguoiChoThue.AutoResizeRows()
    End Sub

    Private Sub btnLayDLtuExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLayDLtuExcel.Click
        Dim fr As New LayDLExcelNguoiChoThue()
        fr.ShowDialog()
        QuanLyNguoiChoThue_Load(sender, e)
    End Sub

    Private Sub btnXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXuat.Click
        Dim gr As SaveFileDialog = New SaveFileDialog()
        Dim FileName As String
        With gr
            .InitialDirectory = "C:\"
            .Filter = "Microsoft Excel Worksheet (*.xls)|*.xls"
            .FilterIndex = 1
        End With
        If gr.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            FileName = gr.FileName
        Else
            Exit Sub
        End If
        'Extracting from database
        Dim col, row As Integer
        Dim Excel As Object = CreateObject("Excel.Application")
        If Excel Is Nothing Then
            MsgBox("m***cel re..", MsgBoxStyle.Critical)
            Return
        End If
        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()
                .Worksheets(1).name = "NguoiChoThue"
                Dim i As Integer = 1
                For col = 0 To DataGridView1_QLNguoiChoThue.Columns.Count - 1
                    .cells(1, i).value = DataGridView1_QLNguoiChoThue.Columns(col).HeaderText
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To DataGridView1_QLNguoiChoThue.Columns.Count - 1
                    i = 2
                    For row = 0 To DataGridView1_QLNguoiChoThue.Rows.Count - 1
                        With .worksheets(1)
                            .Cells(i, k).Value = DataGridView1_QLNguoiChoThue.Rows(row).Cells(col).Value
                            i += 1
                        End With
                    Next
                    k += 1
                Next
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With

            Excel = Nothing
            MsgBox("Dữ liệu đã được xuất sang định dạng Excel thành công!", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXC EL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub

    Private Sub LayDuLieu(ByVal NguoiChoThue1 As NguoiChoThue_DTO)

        NguoiChoThue1.MaNha = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(0).FormattedValue.ToString()
        NguoiChoThue1.TenNguoiChoThue = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(1).FormattedValue.ToString()
        NguoiChoThue1.DiaChiChothue = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(2).FormattedValue.ToString()
        NguoiChoThue1.DienThoaiChoThue = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(3).FormattedValue.ToString()
        NguoiChoThue1.GiaChothue = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(4).FormattedValue.ToString()
        NguoiChoThue1.SoNguoiO = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(5).FormattedValue.ToString()
        NguoiChoThue1.MieuTaNha = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(6).FormattedValue.ToString()
        NguoiChoThue1.TieuDe = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(7).FormattedValue.ToString()

    End Sub
    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim nguoichothue1 As New NguoiChoThue_DTO
        If MessageBox.Show("có chắt chắn bạn muốn cập nhật dòng này không?", "xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
            LayDuLieu(nguoichothue1)
            NhaChoThue_BUS.updateDuLieu(nguoichothue1)
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim MaNguoiChoThue As Integer
        Dim trangthai As Boolean
        If MessageBox.Show("có chắt chắn bạn muốn xoá dòng này không", "xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
            MaNguoiChoThue = DataGridView1_QLNguoiChoThue.CurrentRow.Cells(0).FormattedValue.ToString()
            trangthai = NhaChoThue_BUS.deleteDuLieu(MaNguoiChoThue)
            If trangthai = True Then
                DataGridView1_QLNguoiChoThue.Rows.Remove(DataGridView1_QLNguoiChoThue.CurrentRow())
                DataGridView1_QLNguoiChoThue.Refresh()
            End If
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub
End Class